{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.0\n",
       "1    2.0\n",
       "2    3.0\n",
       "3    NaN\n",
       "4    7.0\n",
       "5    8.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "Python Numpy Panda 基础\n",
    "熟悉就好\n",
    "'''\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 创建 Series 可以理解为封装的数组List\n",
    "s = pd.Series([1,2,3, np.nan, 7,8])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-09-20', '2019-09-21', '2019-09-22', '2019-09-23',\n",
       "               '2019-09-24', '2019-09-25'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建DataFrame 多维数组 index columns\n",
    "datas = pd.date_range('20190920', periods=6)\n",
    "datas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096\n",
       "2019-09-23 -0.175478 -0.264679  0.634894 -0.032379\n",
       "2019-09-24 -0.392577 -0.068133 -0.455599  0.795885\n",
       "2019-09-25 -1.727496 -1.309325 -0.743799 -0.314621"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(6,4), index=datas, columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-20</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A          B    C  D      E    F\n",
       "0  1 2019-09-20  1.0  3   test  foo\n",
       "1  1 2019-09-20  1.0  3  train  foo\n",
       "2  1 2019-09-20  1.0  3   test  foo\n",
       "3  1 2019-09-20  1.0  3  train  foo"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame({'A':1,\n",
    "                    'B':pd.Timestamp('20190920'),\n",
    "                    'C':pd.Series(1, index=list(range(4)), dtype='float32'),\n",
    "                    'D': np.array([3] * 4, dtype='int32'),\n",
    "                    'E': pd.Categorical([\"test\", \"train\", \"test\", \"train\"]),\n",
    "                    'F': 'foo'})\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A             int64\n",
       "B    datetime64[ns]\n",
       "C           float32\n",
       "D             int32\n",
       "E          category\n",
       "F            object\n",
       "dtype: object"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 基本的操作\n",
    "# 查看基本的数据\n",
    "df.head(3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-23 -0.175478 -0.264679  0.634894 -0.032379\n",
       "2019-09-24 -0.392577 -0.068133 -0.455599  0.795885\n",
       "2019-09-25 -1.727496 -1.309325 -0.743799 -0.314621"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2019-09-20', '2019-09-21', '2019-09-22', '2019-09-23',\n",
       "               '2019-09-24', '2019-09-25'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C', 'D'], dtype='object')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.66345536,  1.53213582, -1.38092911,  0.59357006],\n",
       "       [-0.92840117, -0.7415642 , -1.41425044,  0.862455  ],\n",
       "       [-2.42439082,  1.27975523, -1.25649159,  0.12309617],\n",
       "       [-0.17547808, -0.26467888,  0.63489429, -0.03237857],\n",
       "       [-0.39257671, -0.068133  , -0.45559882,  0.79588516],\n",
       "       [-1.72749636, -1.30932547, -0.7437985 , -0.31462104]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.to_numpy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-1.051966</td>\n",
       "      <td>0.071365</td>\n",
       "      <td>-0.769362</td>\n",
       "      <td>0.338001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.861293</td>\n",
       "      <td>1.121927</td>\n",
       "      <td>0.787176</td>\n",
       "      <td>0.481520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-1.527723</td>\n",
       "      <td>-0.622343</td>\n",
       "      <td>-1.349820</td>\n",
       "      <td>0.006490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-0.795928</td>\n",
       "      <td>-0.166406</td>\n",
       "      <td>-1.000145</td>\n",
       "      <td>0.358333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-0.460296</td>\n",
       "      <td>0.942783</td>\n",
       "      <td>-0.527649</td>\n",
       "      <td>0.745306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  6.000000  6.000000  6.000000  6.000000\n",
       "mean  -1.051966  0.071365 -0.769362  0.338001\n",
       "std    0.861293  1.121927  0.787176  0.481520\n",
       "min   -2.424391 -1.309325 -1.414250 -0.314621\n",
       "25%   -1.527723 -0.622343 -1.349820  0.006490\n",
       "50%   -0.795928 -0.166406 -1.000145  0.358333\n",
       "75%   -0.460296  0.942783 -0.527649  0.745306\n",
       "max   -0.175478  1.532136  0.634894  0.862455"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 展示快速的数据总结\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2019-09-20</th>\n",
       "      <th>2019-09-21</th>\n",
       "      <th>2019-09-22</th>\n",
       "      <th>2019-09-23</th>\n",
       "      <th>2019-09-24</th>\n",
       "      <th>2019-09-25</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-2.424391</td>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-1.727496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.532136</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-1.309325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.380929</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>-0.743799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>0.593570</td>\n",
       "      <td>0.862455</td>\n",
       "      <td>0.123096</td>\n",
       "      <td>-0.032379</td>\n",
       "      <td>0.795885</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2019-09-20  2019-09-21  2019-09-22  2019-09-23  2019-09-24  2019-09-25\n",
       "A   -0.663455   -0.928401   -2.424391   -0.175478   -0.392577   -1.727496\n",
       "B    1.532136   -0.741564    1.279755   -0.264679   -0.068133   -1.309325\n",
       "C   -1.380929   -1.414250   -1.256492    0.634894   -0.455599   -0.743799\n",
       "D    0.593570    0.862455    0.123096   -0.032379    0.795885   -0.314621"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置\n",
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-25 -1.727496 -1.309325 -0.743799 -0.314621\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455\n",
       "2019-09-23 -0.175478 -0.264679  0.634894 -0.032379\n",
       "2019-09-24 -0.392577 -0.068133 -0.455599  0.795885\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用某一行排序\n",
    "df.sort_values(by='B')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2019-09-20   -0.663455\n",
       "2019-09-21   -0.928401\n",
       "2019-09-22   -2.424391\n",
       "2019-09-23   -0.175478\n",
       "2019-09-24   -0.392577\n",
       "2019-09-25   -1.727496\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取单独的一列\n",
    "df['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某几行\n",
    "df[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2019-09-20':'2019-09-22']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.663455\n",
       "B    1.532136\n",
       "C   -1.380929\n",
       "D    0.593570\n",
       "Name: 2019-09-20 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过index获取某一行\n",
    "df.loc['2019-09-20']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>-1.380929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-1.414250</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         C\n",
       "2019-09-20 -0.663455 -1.380929\n",
       "2019-09-21 -0.928401 -1.414250"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取针对的某行某列\n",
    "df.loc['2019-09-20':'2019-09-21', ['A','C']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.6634553634971773"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取特定位置的值\n",
    "df.at['2019-09-20','A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.175478\n",
       "B   -0.264679\n",
       "C    0.634894\n",
       "D   -0.032379\n",
       "Name: 2019-09-23 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过位置获取某一列\n",
    "df.iloc[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2019-09-23 -0.175478 -0.264679\n",
       "2019-09-24 -0.392577 -0.068133"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过位置索引\n",
    "df.iloc[3:5,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.7415641974678049"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过位置获取特定的值\n",
    "df.iat[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对特定的列过滤\n",
    "df[df.B>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             A         B         C         D\n",
       "2019-09-20 NaN  1.532136       NaN  0.593570\n",
       "2019-09-21 NaN       NaN       NaN  0.862455\n",
       "2019-09-22 NaN  1.279755       NaN  0.123096\n",
       "2019-09-23 NaN       NaN  0.634894       NaN\n",
       "2019-09-24 NaN       NaN       NaN  0.795885\n",
       "2019-09-25 NaN       NaN       NaN       NaN"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df >0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D  E\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570  1\n",
       "2019-09-21 -0.928401 -0.741564 -1.414250  0.862455  2\n",
       "2019-09-22 -2.424391  1.279755 -1.256492  0.123096  3\n",
       "2019-09-23 -0.175478 -0.264679  0.634894 -0.032379  4\n",
       "2019-09-24 -0.392577 -0.068133 -0.455599  0.795885  5\n",
       "2019-09-25 -1.727496 -1.309325 -0.743799 -0.314621  6"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新增一列 这种方式要保证每一列数量一致\n",
    "df3=df.copy(deep=True)\n",
    "df3['E']=['1','2','3','4','5','6']\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-09-20</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-21</th>\n",
       "      <td>-1.591857</td>\n",
       "      <td>0.790572</td>\n",
       "      <td>-2.795180</td>\n",
       "      <td>1.456025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-22</th>\n",
       "      <td>-4.016247</td>\n",
       "      <td>2.070327</td>\n",
       "      <td>-4.051671</td>\n",
       "      <td>1.579121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-23</th>\n",
       "      <td>-4.191725</td>\n",
       "      <td>1.805648</td>\n",
       "      <td>-3.416777</td>\n",
       "      <td>1.546743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-24</th>\n",
       "      <td>-4.584302</td>\n",
       "      <td>1.737515</td>\n",
       "      <td>-3.872376</td>\n",
       "      <td>2.342628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-09-25</th>\n",
       "      <td>-6.311799</td>\n",
       "      <td>0.428190</td>\n",
       "      <td>-4.616174</td>\n",
       "      <td>2.028007</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2019-09-20 -0.663455  1.532136 -1.380929  0.593570\n",
       "2019-09-21 -1.591857  0.790572 -2.795180  1.456025\n",
       "2019-09-22 -4.016247  2.070327 -4.051671  1.579121\n",
       "2019-09-23 -4.191725  1.805648 -3.416777  1.546743\n",
       "2019-09-24 -4.584302  1.737515 -3.872376  2.342628\n",
       "2019-09-25 -6.311799  0.428190 -4.616174  2.028007"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对数据应用function 累加赋值\n",
    "df.apply(np.cumsum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.663455</td>\n",
       "      <td>1.532136</td>\n",
       "      <td>-1.380929</td>\n",
       "      <td>0.593570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.928401</td>\n",
       "      <td>-0.741564</td>\n",
       "      <td>-1.414250</td>\n",
       "      <td>0.862455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-2.424391</td>\n",
       "      <td>1.279755</td>\n",
       "      <td>-1.256492</td>\n",
       "      <td>0.123096</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-0.175478</td>\n",
       "      <td>-0.264679</td>\n",
       "      <td>0.634894</td>\n",
       "      <td>-0.032379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>-0.392577</td>\n",
       "      <td>-0.068133</td>\n",
       "      <td>-0.455599</td>\n",
       "      <td>0.795885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-1.727496</td>\n",
       "      <td>-1.309325</td>\n",
       "      <td>-0.743799</td>\n",
       "      <td>-0.314621</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           A         B         C         D\n",
       "0  -0.663455  1.532136 -1.380929  0.593570\n",
       "1  -0.928401 -0.741564 -1.414250  0.862455\n",
       "2  -2.424391  1.279755 -1.256492  0.123096\n",
       "3  -0.175478 -0.264679  0.634894 -0.032379\n",
       "4  -0.392577 -0.068133 -0.455599  0.795885\n",
       "5  -1.727496 -1.309325 -0.743799 -0.314621\n",
       "6  -0.663455  1.532136 -1.380929  0.593570\n",
       "7  -0.928401 -0.741564 -1.414250  0.862455\n",
       "8  -2.424391  1.279755 -1.256492  0.123096\n",
       "9  -0.175478 -0.264679  0.634894 -0.032379\n",
       "10 -0.392577 -0.068133 -0.455599  0.795885\n",
       "11 -1.727496 -1.309325 -0.743799 -0.314621"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  拼接数据 行累计\n",
    "pd.concat([df,df])\n",
    "df.append(df, ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  foo     1     5\n",
       "2  foo     2     4\n",
       "3  foo     2     5"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 拼接数据 列累计\n",
    "left = pd.DataFrame({'key': ['foo', 'foo'], 'lval': [1, 2]})\n",
    "right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})\n",
    "pd.merge(left, right, on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  bar     2     5"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})\n",
    "right = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})\n",
    "pd.merge(left, right, on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',\n",
    "                           'foo', 'bar', 'foo', 'foo'],\n",
    "                     'B': ['one', 'one', 'two', 'three',\n",
    "                            'two', 'two', 'one', 'three'],\n",
    "                    'C': np.random.randn(8),\n",
    "                  'D': np.random.randn(8)})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <td>1.598841</td>\n",
       "      <td>1.084794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <td>1.134215</td>\n",
       "      <td>-2.912552</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            C         D\n",
       "A                      \n",
       "bar  1.598841  1.084794\n",
       "foo  1.134215 -2.912552"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照组进行计算\n",
    "df.groupby('A').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.474336</td>\n",
       "      <td>2.242205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>0.233636</td>\n",
       "      <td>-0.147263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.890868</td>\n",
       "      <td>-1.010148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.029794</td>\n",
       "      <td>-5.111911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>0.167650</td>\n",
       "      <td>1.552552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.996359</td>\n",
       "      <td>0.646807</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  C         D\n",
       "A   B                        \n",
       "bar one    0.474336  2.242205\n",
       "    three  0.233636 -0.147263\n",
       "    two    0.890868 -1.010148\n",
       "foo one   -0.029794 -5.111911\n",
       "    three  0.167650  1.552552\n",
       "    two    0.996359  0.646807"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " df.groupby(['A', 'B']).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
